package com.smart.platform.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

    import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Component
public class UploadUtil {
    //通过Spring的autowired注解获取spring默认配置的request
    @Autowired
    private HttpServletRequest request;


    public List filesUpload(MultipartFile[] files) {
        List fileNames = new ArrayList();
        //判断file数组不能为空并且长度大于0
        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                //保存文件
                String fileName = saveFile(file);
                //String fileName = "/boot01/upload/upload_" + file.getOriginalFilename();
                if(fileName != null){
                    fileNames.add(fileName);
                }
            }
            System.out.println(fileNames);
            return fileNames;
        }
        // 重定向
        return null;
    }

    /***
     * 保存单个文件
     * @param file
     * @return
     */
    private String saveFile(MultipartFile file) {
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                String path = HelperClazz.UPLOAD_PATH + file.getOriginalFilename();
                //上传
                file.transferTo(new File(path));
                String fileName = file.getOriginalFilename();
                return fileName;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /***
     * 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile
     *
     * @param file
     * @return
     */
    public String fileUpload(MultipartFile file) {
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                // 文件保存路径
                String path = HelperClazz.UPLOAD_PATH + file.getOriginalFilename();
                //上传
                file.transferTo(new File(path));
//            String fileName = file.getOriginalFilename();
//			 request.setAttribute("fileName", fileName);
                return path;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 重定向
        return null;
    }

    /***
     * 读取上传文件中得所有文件并返回
     *
     * @return
     */
    @RequestMapping("list")
    public ModelAndView list() {
        String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
        ModelAndView mav = new ModelAndView("list");
        File uploadDest = new File(filePath);
        String[] fileNames = uploadDest.list();
        for (int i = 0; i < fileNames.length; i++) {
            System.out.println(fileNames[i]);
        }
        return mav;
    }

    public static String byte2hex(byte[] b) // 二进制转字符串
    {
        StringBuffer sb = new StringBuffer();
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1){
                sb.append("0" + stmp);
            }else{
                sb.append(stmp);
            }

        }
        return sb.toString();
    }

    public static byte[] hex2byte(String str) { // 字符串转二进制
        if (str == null)
            return null;
        str = str.trim();
        int len = str.length();
        if (len == 0 || len % 2 == 1)
            return null;
        byte[] b = new byte[len / 2];
        try {
            for (int i = 0; i < str.length(); i += 2) {
                b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();
            }
            return b;
        } catch (Exception e) {
            return null;
        }
    }
}
